package ai4games_tests.maths;

import game2dai.maths.Geometry2D;
import processing.core.PApplet;


public class LineCircleIntersection extends PApplet {

	float x1,y1,x2,y2,cx,cy,r;
	float[] ipoint = null;
	boolean intersect;

	public void setup(){
		size(400,300);
		cursor(CROSS);
		cx = width/2;
		cy = height/2;
		r = 30;
		x1 = 100;
		y1 = 100;
		x2 = width - 100;
		y2 = height - 100;
		intersect = false;
	}

	public void draw(){
		background(128);
		cx = mouseX;
		cy = mouseY;		
		intersect = Geometry2D.line_circle(x1,y1,x2,y2,cx,cy,r);
		ipoint = makeFloatArray(Geometry2D.line_circle_p(x1,y1,x2,y2,cx,cy,r));
		ellipseMode(CENTER);
		strokeWeight(1.5f);
		stroke(0,255,0);
		fill(255,255,192);
		if(intersect)
			stroke(255,0,0);
		ellipse(cx,cy,2*r,2*r);
		line(x1,y1,x2,y2);
		
		fill(200,200,255);
		noStroke();
		for(int p = 0; p < ipoint.length; p += 2)
			ellipse(ipoint[p], ipoint[p+1], 10,10);
		fill(255,255,0);
		text("Number of intersections = " + ipoint.length/2, 10, height - 4);
	}

	public float[] makeFloatArray(double[] da){
		float[] fa = new float[da.length];
		for(int i = 0; i < fa.length; i++)
			fa[i] = (float)da[i];
		return fa;
	}
}
